//
//  SEGAuthorStatusViewModelS.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/24.
//

#import "SEGAuthorStatusViewModelS.h"
@interface SEGAuthorStatusViewModelS()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;

@property (nonatomic, strong, readwrite) RACCommand *cancelOperCommand;
@end

@implementation SEGAuthorStatusViewModelS
#pragma mark - Init
-(instancetype)init{
    if (self = [super init]) {
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
    
    [self.cancelOperCommand rac_deallocDisposable];
}

- (void)racInit {

    //@weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);

            // Code here...
            [[SEGUserselectbyuserManager sharedManager]requestUserselectbyuserWithCommunityid:kAccountCommunityID userId:kAccountUserID finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return;
            }];

            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _cancelOperCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            //@strongify(self);

            // Code here...
            SEGUserselectbyuserListModel *userSelectByUser = [SEGUserselectbyuserManager sharedManager].UserSelectByUser.data.list.firstObject;
            [[SEGUsercancelapplicationManager sharedManager] requestUsercancelapplicationWithEnterpriseuserid:userSelectByUser.enterpriseId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                    return;
                }
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return;
            }];

            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowSVP:(BOOL)show {
    [self.messageSubject sendNext:@{@"code": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"3",@"msg":msg}];
}
#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
